package net.gzw1993.runmate.adapter;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.bean.Recorder;
import net.gzw1993.runmate.bean.UserInfo;
import net.gzw1993.runmate.utils.MyBitmapUtilsHelper;

/**
 * Created by guoziwei on 2015/8/19.
 */
public class RecorderAdapter extends SimpleBaseAdapter<Recorder> {

    private UserInfo userInfo;

    public RecorderAdapter(Context context, UserInfo userInfo) {
        super(context);
        this.userInfo = userInfo;
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        Recorder recorder = mList.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_recorder_list, parent, false);
            ViewHolder holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.createTime.setText(DateUtils.formatDateTime(mContext, recorder.getCreateTime(),
                DateUtils.FORMAT_ABBREV_ALL));
        holder.destance.setText(recorder.getDistance() + "km");
        BitmapUtils bitmapUtils = MyBitmapUtilsHelper.getInstance().getBitmapUtils();
        bitmapUtils.display(holder.icon, userInfo.getIcon());
        bitmapUtils.display(holder.img, recorder.getImg());
        holder.location.setText(recorder.getLocation() == null ? "" : recorder.getLocation());
        holder.name.setText(userInfo.getName());
        holder.pace.setText(recorder.getPace());
        holder.totalSeconds.setText(recorder.getTotalSeconds() + "");

        return convertView;
    }

    private static class ViewHolder {
        @ViewInject(R.id.recorder_iv_icon)
        ImageView icon;
        @ViewInject(R.id.recorder_iv_img)
        ImageView img;
        @ViewInject(R.id.recorder_tv_createTime)
        TextView createTime;
        @ViewInject(R.id.recorder_tv_distance)
        TextView destance;
        @ViewInject(R.id.recorder_tv_location)
        TextView location;
        @ViewInject(R.id.recorder_tv_name)
        TextView name;
        @ViewInject(R.id.recorder_tv_pace)
        TextView pace;
        @ViewInject(R.id.recorder_tv_totalSeconds)
        TextView totalSeconds;

        public ViewHolder(View v) {
            ViewUtils.inject(this, v);
        }
    }
}
